<?php
/**
 * 
 * @package	educME
 * @copyright (c) Jose Raul Perez 2011 under GPL
 */

// error_reporting(E_ALL);
// ini_set("display_errors", 1);
 
require_once (dirname(__FILE__) . '/../classes/MoodleWS.php');
require_once (dirname(__FILE__) . '/../auth.php');


function getAssignment($coursemodule) {

    $client = new MoodleWS();
    $lr = $client->login(LOGIN, PASSWORD);

    $courseids = array();

    // TODO To search better way to find real id, maybe changing mysql query
    $res = $client->get_instances_bytype($lr->getClient(),$lr->getSessionKey(),$courseids,'','assignment');
    $assignment = "";
 //   var_dump($res->getResources());
    foreach ($res->getResources() as $assign) {
        if ($assign->coursemodule == $coursemodule) {
            $assignment = $client->get_all_assignments($lr->getClient(), $lr->getSessionKey(), 'id', $assign->id);
            break;
        }
    }
	
    $client->logout($lr->getClient(), $lr->getSessionKey());

    return $assignment;
}

function getSubmission($coursemodule) {
    $client = new MoodleWS();
    $lr = $client->login(LOGIN, PASSWORD);

    // TODO To search better way to find real id, maybe changing mysql query
    $res = $client->get_instances_bytype($lr->getClient(), $lr->getSessionKey(), array(), '', 'assignment');
    $assignment = "";
    foreach ($res->getResources() as $assign) {
        if ($assign->coursemodule == $coursemodule) {
            $res = $client->get_assignment_submissions($lr->getClient(),$lr->getSessionKey(), $assign->id, '','',0);
            break;
        }
    }
    
    $client->logout($lr->getClient(), $lr->getSessionKey());

    return $res->getSubmissions();
}

function updateSubmission($data) {
    $client = new MoodleWS();
    $lr = $client->login(LOGIN, PASSWORD);

    $submission = new assignmentSubmissionRecord(
            '',
            $data["id"],
            $data["assignment"],
            $data["assignmenttype"],
            $data["userid"],
            $data["timecreated"],
            $data["timemodified"],
            $data["numfiles"],
            $data["data1"],
            $data["data2"],
            $data["grade"],
            $data["submissioncomment"],
            $data["format"],
            $data["teacher"],
            $data["timemarked"],
            $data["mailed"],
            $data["useridnumber"],
            $data["userusername"],
            $data["useremail"],
            $data["files"]
    );


    if ($submission->assignmenttype != "online") {
        // TODO very ugly, bucle 
        $currentFile = FSI::getFile($submission->files[0]["filepath"]);

        $file = new StdClass;
        $file->fileurl = "";
        $file->filename = $currentFile->getName();
        $file->filepath = "/";
        $file->filesize = "";
        // encode for solve utf8 error in SOAP transaction
        $file->filecontent = base64_encode($currentFile->getContents());

        $files = array($file);

        $submission->setFiles($files);
    }

    $temp = $client->update_submission($lr->getClient(),$lr->getSessionKey(), $submission);
        
    $client->logout($lr->getClient(), $lr->getSessionKey());

    return $temp;
}

?>